//
//  WithDrawListVC.swift
//  kxf
//  提现记录
//  Created by Mac on 2024/3/12.
//

import Foundation
import MCToast
import MJRefresh

class WithDrawListVC: WHBaseViewVC {
    
    
    var serviceType = "2"
    var pageNum = 1
    var pageSize = 5
    
    var dataSourceArray = NSMutableArray()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendDataListRequest()
    }
    
    lazy var tableView : UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(8), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-(getNavigationBarHeight()+kFitWidth(8))), style: .plain)
        vi.delegate = self
        vi.dataSource = self
        vi.separatorStyle = .none
        vi.backgroundColor = .COLOR_LIGHT_GREY
        
        vi.register(WithDrawListTableViewCell.classForCoder(), forCellReuseIdentifier: "WithDrawListTableViewCell")
        
        if #available(iOS 11.0, *) {
            vi.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        vi.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.pageNum = 1
            self.sendDataListRequest()
        })

        vi.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {
            self.pageNum += 1
            self.sendDataListRequest()
        })
        return vi
    }()
    lazy var emptyView : EmptyViewWithBtn = {
        let emptyViewT = EmptyViewWithBtn.init(frame: CGRect.zero)
        emptyViewT.isHidden = true
        emptyViewT.contentLabel.text = "还没有记录~"
        
        return emptyViewT
    }()
}

extension WithDrawListVC{
    func initUI()  {
        initNavi(titleStr: "提现记录")
        
        view.addSubview(tableView)
        tableView.addSubview(emptyView)
        emptyView.center = CGPoint.init(x: SCREEN_WIDHT*0.5, y: self.tableView.frame.height*0.5)
    }
}

extension WithDrawListVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        emptyView.isHidden = dataSourceArray.count > 0 ? true : false
        return dataSourceArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "WithDrawListTableViewCell") as! WithDrawListTableViewCell
        
        let dict = self.dataSourceArray[indexPath.row]as? NSDictionary ?? [:]
        cell.updateUI(dict: dict)
        
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return kFitWidth(116)
    }
}

extension WithDrawListVC{
    func sendDataListRequest() {
        MCToast.mc_loading()
        let urlString = "\(URL_withDraw_list)?serviceType=\(serviceType)&pageNum=\(pageNum)&pageSize=\(pageSize)"
        WHNetworkUtil.shareManager().GET(urlString: urlString) { responseObject in
            DLLog(message: "\(responseObject)")
            MCToast.mc_remove()
            if responseObject["code"]as? Int ?? 0 == 200 {
                self.tableView.mj_footer!.endRefreshing()
                self.tableView.mj_header!.endRefreshing()
                
                let dataDict = responseObject["data"]as? NSDictionary ?? [:]
                let dataArr = dataDict["list"]as? NSArray ?? []
                
                if self.pageNum == 1{
                    self.dataSourceArray.removeAllObjects()
                }
                // 提示没有更多数据
                if dataArr.count < self.pageSize {
                    self.tableView.mj_footer!.endRefreshingWithNoMoreData()
                }
                
                self.dataSourceArray.addObjects(from: dataArr as! [Any])
                self.tableView.reloadData()
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "")", viewController: self)
            }
        }
    }
}
